package com.fancy.music.controller;

import com.fancy.common.dto.ArtistBasicInfo;
import com.fancy.common.dto.PageResult;
import com.fancy.common.dto.response.Result;
import com.fancy.music.dto.ArtistDetailDTO;
import com.fancy.music.entity.Artist;
import com.fancy.music.service.ArtistService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "歌手管理")
@RestController
@RequestMapping("/api/v1/artists")
@RequiredArgsConstructor
public class ArtistController {

    private final ArtistService artistService;

    @ApiOperation(value = "搜索歌手", notes = "根据关键词、风格、国家、性别等条件搜索歌手")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyword", value = "搜索关键词", paramType = "query", dataType = "string", example = "周杰伦"),
        @ApiImplicitParam(name = "genre", value = "音乐风格", paramType = "query", dataType = "string", example = "流行"),
        @ApiImplicitParam(name = "country", value = "国家/地区", paramType = "query", dataType = "string", example = "中国"),
        @ApiImplicitParam(name = "gender", value = "性别(1-男,2-女,3-组合)", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int", example = "20")
    })
    @GetMapping("/search")
    public Result<PageResult<Artist>> searchArtists(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) Integer gender,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        PageResult<Artist> result = artistService.searchArtists(keyword, genre, country, gender, page, size);
        return Result.success(result);
    }

    @ApiOperation(value = "获取歌手详情", notes = "根据歌手ID获取歌手的详细信息")
    @ApiImplicitParam(name = "artistId", value = "歌手ID", required = true, paramType = "path", dataType = "long", example = "1")
    @GetMapping("/{artistId}")
    public Result<ArtistDetailDTO> getArtistDetail(@PathVariable Long artistId) {
        ArtistDetailDTO artist = artistService.getArtistDetailWithInfo(artistId);
        if (artist != null) {
            return Result.success(artist);
        } else {
            return Result.fail("歌手不存在");
        }
    }

    @ApiOperation(value = "获取热门歌手", notes = "获取热门歌手列表")
    @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    @GetMapping("/hot")
    public Result<List<ArtistDetailDTO>> getHotArtists(@RequestParam(defaultValue = "10") Integer limit) {
        List<ArtistDetailDTO> artists = artistService.getHotArtistsWithDetails(limit);
        return Result.success(artists);
    }

    @ApiOperation(value = "获取新人歌手", notes = "获取新出道的歌手列表")
    @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    @GetMapping("/new")
    public Result<List<ArtistDetailDTO>> getNewArtists(@RequestParam(defaultValue = "10") Integer limit) {
        List<ArtistDetailDTO> artists = artistService.getNewArtistsWithDetails(limit);
        return Result.success(artists);
    }

    @Deprecated
    @ApiOperation(value = "按风格获取歌手", notes = "根据音乐风格获取歌手列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "genre", value = "音乐风格", required = true, paramType = "query", dataType = "string", example = "流行"),
        @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    })
    @GetMapping("/by-genre")
    public Result<List<ArtistDetailDTO>> getArtistsByGenre(
            @RequestParam String genre,
            @RequestParam(defaultValue = "10") Integer limit) {
        List<ArtistDetailDTO> artists = artistService.getArtistsByGenreWithDetails(genre, limit);
        return Result.success(artists);
    }

    @ApiOperation(value = "按国家获取歌手", notes = "根据国家/地区获取歌手列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "country", value = "国家/地区", required = true, paramType = "query", dataType = "string", example = "中国"),
        @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    })
    @GetMapping("/by-country")
    public Result<List<ArtistDetailDTO>> getArtistsByCountry(
            @RequestParam String country,
            @RequestParam(defaultValue = "10") Integer limit) {
        List<ArtistDetailDTO> artists = artistService.getArtistsByCountryWithDetails(country, limit);
        return Result.success(artists);
    }

    // 内部接口，不显示在API文档中
    @ApiIgnore
    @GetMapping("/internal/{artistId}")
    public Result<ArtistBasicInfo> getArtistBasicInfo(@PathVariable Long artistId) {
        ArtistBasicInfo info = artistService.getArtistBasicInfo(artistId);
        return Result.success(info);
    }

    @ApiIgnore
    @PostMapping("/internal/batch")
    public Result<List<ArtistBasicInfo>> getArtistBasicInfoBatch(@RequestBody List<Long> artistIds) {
        List<ArtistBasicInfo> infos = artistService.getArtistBasicInfoBatch(artistIds);
        return Result.success(infos);
    }

    @ApiOperation(value = "获取所有歌手音乐风格", notes = "获取系统中所有歌手的音乐风格标签")
    @GetMapping("/genres")
    public Result<List<String>> getAllArtistGenres() {
        List<String> genres = artistService.getAllArtistGenres();
        return Result.success(genres);
    }

    @ApiOperation(value = "根据音乐风格获取歌手", notes = "根据音乐风格获取匹配的歌手列表（支持部分匹配）")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "genre", value = "音乐风格", required = true, paramType = "query", dataType = "string", example = "华语流行"),
        @ApiImplicitParam(name = "limit", value = "返回数量限制", paramType = "query", dataType = "int", example = "10")
    })
    @GetMapping("/by-genre-match")
    public Result<List<ArtistDetailDTO>> getArtistsByGenreMatch(
            @RequestParam String genre,
            @RequestParam(defaultValue = "10") Integer limit) {
        List<ArtistDetailDTO> artists = artistService.getArtistsByGenreMatchWithDetails(genre, limit);
        return Result.success(artists);
    }
}